home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / win_a_d / dyndlg.zip / WNDPROC.C < prev    next >
Text File  |  1992-10-08  |  13KB  |  363 lines

  1. //*-------------------------------------------------------------------------
  2. //| Title:
  3. //|     WNDPROC.C
  4. //|
  5. //| Contents:
  6. //|     MainWndProc()
  7. //*-------------------------------------------------------------------------
  8. #include <WINDOWS.H>
  9. #include "WNDPROC.H"
  10. #include "DynDlg.H"
  11. #include "GLOBALS.H"
  12.  
  13. //*-------------------------------------------------------------------------
  14. //| Title:
  15. //|     MainWndProc
  16. //|
  17. //| Parameters:
  18. //|     hWnd            - Handle to the message's destination window
  19. //|     wMessage        - Message number of the current message
  20. //|     wParam          - Additional info associated with the message
  21. //|     lParam          - Additional info associated with the message
  22. //|
  23. //| Purpose:
  24. //|     This is the window procedure for the application's main window.
  25. //*-------------------------------------------------------------------------
  26. LRESULT FAR PASCAL MainWndProc(HWND    hWnd,       
  27.                                UINT    wMessage,   
  28.                                WPARAM  wParam,     
  29.                                LPARAM  lParam)     
  30. {
  31.     switch (wMessage)
  32.         {
  33.  
  34.         case WM_COMMAND:
  35.         {
  36.             FARPROC lpProcDlg;
  37.  
  38.             if (wParam == IDM_ABOUT) {
  39.                 lpProcDlg = MakeProcInstance(AboutBox, ghInstance);
  40.  
  41.                 DialogBox(ghInstance,
  42.                     "AboutBox",
  43.                     hWnd,
  44.                     lpProcDlg);
  45.  
  46.                 FreeProcInstance(lpProcDlg);
  47.                 break;
  48.             }
  49.             else
  50.             if (wParam == IDM_DIALOG) {
  51.                 lpProcDlg = MakeProcInstance(SpecialDlgBox, ghInstance);
  52.  
  53.                 DialogBox(ghInstance,
  54.                     "SpecialDlgBox",
  55.                     hWnd,
  56.                     lpProcDlg);
  57.  
  58.                 FreeProcInstance(lpProcDlg);
  59.                 break;
  60.             }
  61.             else
  62.                 return (LRESULT)(DefWindowProc(hWnd, wMessage, wParam, lParam));
  63.         }
  64.  
  65.         case WM_DESTROY:          // sent when window about to be destroyed
  66.             PostQuitMessage(0);   // Indicate that message loop should exit
  67.             break;                //   since the main window is being destroyed
  68.  
  69.         default:                  // Pass message on for default proccessing
  70.             return (LRESULT)DefWindowProc(hWnd, wMessage, wParam, lParam);
  71.         }
  72.  
  73.     // If we performed non-default processing on the message, return FALSE
  74.     return (LRESULT)FALSE;
  75. }
  76.  
  77.  
  78. BOOL FAR PASCAL AboutBox (HWND hDlg,
  79.                           WORD message,
  80.                           WORD wParam,
  81.                           LONG lParam)
  82. {
  83.  
  84.     switch (message) 
  85.     {
  86.  
  87.         case WM_INITDIALOG:
  88.             return (TRUE);
  89.   
  90.         case WM_COMMAND:
  91.             if (wParam == IDOK  || wParam == IDCANCEL) 
  92.             {
  93.                 EndDialog(hDlg, TRUE);
  94.                 return (TRUE);
  95.             }
  96.             break;
  97.             
  98.     }
  99.     return (FALSE);
  100. }
  101.  
  102. void ShowAllControls (HWND hDlg, int iFirst, int iLast, BOOL bShow)
  103. {
  104.   int  i;
  105.   HWND hFrame;
  106.  
  107.  
  108.   hFrame = GetDlgItem (hDlg, ID_FRAME);
  109.   SendMessage (hFrame, WM_SETREDRAW, FALSE, 0L);
  110.  
  111.   for (i = iFirst; i < iLast; i++)
  112.     ShowWindow (GetDlgItem (hDlg, i), bShow);
  113.  
  114. //InvalidateRect (hFrame, NULL, TRUE);
  115.   SendMessage (hFrame, WM_SETREDRAW, TRUE, 0L);
  116. }
  117.  
  118.  
  119. void AddIconToListBox (HWND hDlg, LPSTR myStr, HICON hIcon)
  120. {
  121.    DWORD dwIndex;
  122.  
  123.    dwIndex = SendDlgItemMessage (hDlg, ID_LISTBOX, LB_ADDSTRING, 0, (LONG)myStr);
  124.    if ((dwIndex != LB_ERR) && (dwIndex != LB_ERRSPACE))
  125.      SendDlgItemMessage (hDlg, ID_LISTBOX, LB_SETITEMDATA, (WPARAM)dwIndex, MAKELONG (hIcon, 0));
  126.  
  127.  
  128. void DrawItem (HWND hDlg, LPDRAWITEMSTRUCT lpdis)
  129. {
  130.    char       szItemString [15];
  131.    HICON      hIcon;
  132.    int        cxIcon, cyIcon;
  133.    RECT       lbRect, textRect;
  134.    TEXTMETRIC tm;
  135.  
  136.    cxIcon = GetSystemMetrics (SM_CXICON);
  137.    cyIcon = GetSystemMetrics (SM_CYICON);
  138.    GetTextMetrics (lpdis->hDC, &tm);
  139.  
  140.    SendDlgItemMessage (hDlg, ID_LISTBOX, LB_GETTEXT, lpdis->itemID,
  141.                       (LONG)(LPSTR)szItemString);
  142.    hIcon = (HICON)LOWORD (SendDlgItemMessage (hDlg, ID_LISTBOX, LB_GETITEMDATA,
  143.                        lpdis->itemID, 0L));
  144.  
  145.    GetClientRect (GetDlgItem (hDlg, ID_LISTBOX), &lbRect);
  146.    
  147.    DrawIcon(lpdis->hDC, lpdis->rcItem.left+(lbRect.right/2) - (cxIcon/2), 
  148.                         lpdis->rcItem.top + (tm.tmHeight/4), hIcon);
  149.  
  150.    textRect.left  = lpdis->rcItem.left;
  151.    textRect.top   = lpdis->rcItem.top + cyIcon + (tm.tmHeight/4);
  152.    textRect.right = lpdis->rcItem.right;    
  153.    textRect.bottom= lpdis->rcItem.bottom;
  154.    DrawText (lpdis->hDC, szItemString, lstrlen (szItemString),
  155.              &textRect, DT_CENTER);
  156.  
  157.  
  158. }
  159.  
  160.  
  161. BOOL FAR PASCAL SpecialDlgBox (HWND hDlg,
  162.                                WORD message,
  163.                                WORD wParam,
  164.                                LONG lParam)
  165. {
  166.     static int curFirst, curLast;
  167.     switch (message) 
  168.     {
  169.         case WM_INITDIALOG:
  170.             AddIconToListBox (hDlg, "FileOpen",     LoadIcon (ghInstance, "FOpenIcon"  ));
  171.             AddIconToListBox (hDlg, "Font",         LoadIcon (ghInstance, "FontIcon"   ));
  172.             AddIconToListBox (hDlg, "PrintSetup",   LoadIcon (ghInstance, "PrSetupIcon"));
  173.             AddIconToListBox (hDlg, "Print",        LoadIcon (ghInstance, "PrintIcon"  ));
  174.             AddIconToListBox (hDlg, "Find",         LoadIcon (ghInstance, "FindIcon"   ));
  175.             AddIconToListBox (hDlg, "Find/Replace", LoadIcon (ghInstance, "FindRepIcon"));
  176.  
  177.             ShowAllControls (hDlg,IDCD_FONTFIRST,    IDCD_FONTLAST,    SW_HIDE);
  178.             ShowAllControls (hDlg,IDCD_PRSETUPFIRST, IDCD_PRSETUPLAST, SW_HIDE);
  179.             ShowAllControls (hDlg,IDCD_PRINTFIRST,   IDCD_PRINTLAST,   SW_HIDE);
  180.             ShowAllControls (hDlg,IDCD_FINDFIRST,    IDCD_FINDLAST,    SW_HIDE);
  181.             ShowAllControls (hDlg,IDCD_REPFIRST,     IDCD_REPLAST,     SW_HIDE);
  182.  
  183.             curFirst = IDCD_FOPENFIRST;
  184.             curLast  = IDCD_FOPENLAST;
  185.             return TRUE;
  186.  
  187.         case WM_MEASUREITEM:
  188.            {
  189.              MEASUREITEMSTRUCT *ms;
  190.              TEXTMETRIC tm;
  191.              HDC  hDC;
  192.              int  cy;
  193.  
  194.              ms = (MEASUREITEMSTRUCT *)lParam;
  195.  
  196.              if (ms->CtlID == ID_LISTBOX)
  197.              {
  198.                cy = GetSystemMetrics (SM_CYICON);
  199.   
  200.                hDC = GetDC (hDlg);
  201.                GetTextMetrics (hDC, &tm);
  202.                ReleaseDC (hDlg, hDC);
  203.   
  204.                ms->itemHeight = cy + (3*tm.tmHeight/2);
  205.                ms->itemWidth  = 58;
  206.              }
  207.            } 
  208.            break;
  209.  
  210.         case WM_DRAWITEM:
  211.            {
  212.              LPDRAWITEMSTRUCT lpdis;
  213.  
  214.              lpdis = (LPDRAWITEMSTRUCT)lParam;
  215.  
  216.              if (lpdis->CtlID == ID_LISTBOX)
  217.              {            
  218.                DWORD  dwBkColor, dwOldBkColor, dwTextColor;
  219.                HBRUSH hBrush, hOldBrush;
  220.  
  221.                if (lpdis->itemID == -1)
  222.                  DrawFocusRect (lpdis->hDC, (LPRECT)&lpdis->rcItem);
  223.  
  224.                if ((lpdis->itemAction & ODA_FOCUS) && 
  225.                    (!lpdis->itemState  & ODS_FOCUS))
  226.                  DrawFocusRect (lpdis->hDC, (LPRECT)&lpdis->rcItem);
  227.  
  228.  
  229.                if (lpdis->itemState & ODS_SELECTED)
  230. //                 InvertRect   (lpdis->hDC, (LPRECT)&lpdis->rcItem);
  231.                {
  232.                  dwBkColor = GetSysColor (COLOR_HIGHLIGHT);
  233.                  dwTextColor=GetSysColor (COLOR_HIGHLIGHTTEXT);
  234.                } 
  235.                else
  236.                { 
  237.                  dwBkColor = GetSysColor (COLOR_WINDOW);
  238.                  dwTextColor=GetSysColor (COLOR_WINDOWTEXT);
  239.                }
  240.  
  241.                dwOldBkColor = SetBkColor (lpdis->hDC, dwBkColor);
  242.                SetTextColor (lpdis->hDC, dwTextColor);
  243.  
  244.                hBrush = CreateSolidBrush (dwBkColor);
  245.                hOldBrush = SelectObject (lpdis->hDC, hBrush);
  246.  
  247.                PatBlt (lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
  248.                                    lpdis->rcItem.right  - lpdis->rcItem.left,
  249.                                    lpdis->rcItem.bottom - lpdis->rcItem.top,
  250.                                    PATCOPY);
  251.  
  252.                DrawItem (hDlg, lpdis);
  253.  
  254.                SetBkColor (lpdis->hDC, dwOldBkColor);       // Reset background color
  255.                DeleteObject (SelectObject (lpdis->hDC, hOldBrush));
  256.  
  257.                if ((lpdis->itemAction & ODA_FOCUS) && 
  258.                    (lpdis->itemState  & ODS_FOCUS))
  259.                  DrawFocusRect (lpdis->hDC, (LPRECT)&lpdis->rcItem);
  260.  
  261.  
  262.              }             
  263.            }
  264.            return TRUE;
  265.  
  266.         case WM_COMMAND:  // wParam == IDOK || IDCANCEL
  267.             if (wParam == IDCD_FOPEN11   || wParam == IDCD_FOPEN12  ||
  268.                 wParam == IDCD_FONT7     || wParam == IDCD_FONT8    ||
  269.                 wParam == IDCD_PRINT17   || wParam == IDCD_PRINT18  ||
  270.                 wParam == IDCD_REP7      || wParam == IDCD_REP10    ||
  271.                 wParam == IDCD_FIND8     || wParam == IDCD_FIND9    ||
  272.                 wParam == IDCD_PRSETUP15 || wParam == IDCD_PRSETUP16)             
  273.             {
  274.                 EndDialog(hDlg, TRUE);
  275.                 return (TRUE);
  276.             }
  277.  
  278.             if (HIWORD(lParam) == LBN_SELCHANGE)
  279.             {
  280.                DWORD dwIndex;
  281.                dwIndex = SendDlgItemMessage (hDlg, ID_LISTBOX, LB_GETCURSEL, 0, 0L);
  282.                if (dwIndex == LB_ERR) break;
  283.  
  284.  
  285.                switch (dwIndex)
  286.                {
  287.                   case IDI_FILEOPEN:
  288.                      if (curFirst == IDCD_FOPENFIRST) return (FALSE);
  289.                      ShowAllControls (hDlg, curFirst, curLast, SW_HIDE);
  290.                      curFirst = IDCD_FOPENFIRST;
  291.                      curLast  = IDCD_FOPENLAST;     
  292.                      break;
  293.  
  294.                   case IDI_FONT:
  295.                      if (curFirst == IDCD_FONTFIRST) return (FALSE);
  296.                      ShowAllControls (hDlg, curFirst, curLast, SW_HIDE);
  297.                      curFirst = IDCD_FONTFIRST;
  298.                      curLast  = IDCD_FONTLAST;     
  299.                      break;
  300.  
  301.                   case IDI_PRSETUP:
  302.                      if (curFirst == IDCD_PRSETUPFIRST) return (FALSE);
  303.                      ShowAllControls (hDlg, curFirst, curLast, SW_HIDE);
  304.                      curFirst = IDCD_PRSETUPFIRST;
  305.                      curLast  = IDCD_PRSETUPLAST;     
  306.                      break;
  307.  
  308.                   case IDI_PRINT:
  309.                      if (curFirst == IDCD_PRINTFIRST) return (FALSE);
  310.                      ShowAllControls (hDlg, curFirst, curLast, SW_HIDE);
  311.                      curFirst = IDCD_PRINTFIRST;
  312.                      curLast  = IDCD_PRINTLAST;     
  313.                      break;
  314.  
  315.                   case IDI_FIND:
  316.                      if (curFirst == IDCD_FINDFIRST) return (FALSE);
  317.                      ShowAllControls (hDlg, curFirst, curLast, SW_HIDE);
  318.                      curFirst = IDCD_FINDFIRST;
  319.                      curLast  = IDCD_FINDLAST;     
  320.                      break;
  321.  
  322.                   case IDI_REPLACE:
  323.                      if (curFirst == IDCD_REPFIRST) return (FALSE);
  324.                      ShowAllControls (hDlg, curFirst, curLast, SW_HIDE);
  325.                      curFirst = IDCD_REPFIRST;
  326.                      curLast  = IDCD_REPLAST;     
  327.                      break;
  328.  
  329.                   default : break;
  330.                }
  331.                ShowAllControls (hDlg, curFirst,  curLast,  SW_SHOW);
  332. //             UpdateWindow( GetDlgItem(hDlg,ID_FRAME) );                  
  333.  
  334.                {
  335. /* DaveE Stuff...
  336.                RECT lbRect, dlgRect;
  337.                GetClientRect (GetDlgItem (hDlg, ID_LISTBOX), &lbRect);
  338.  
  339.                // This hack eliminates flicker
  340.  
  341.                ValidateRect (hDlg, NULL );              // First, validate everything
  342.  
  343.                GetClientRect (hDlg, &dlgRect);          // Then get dlg rect
  344.                dlgRect.left = lbRect.right + 1;         // trim off left listbox
  345.                InvalidateRect (hDlg, &dlgRect, FALSE);  // invalidate the rest
  346. */
  347.                InvalidateRect (GetDlgItem (hDlg, ID_FRAME), NULL, FALSE);  // invalidate the rest
  348.                }
  349.             }
  350.             break;
  351.  
  352.        case WM_CLOSE:
  353.             EndDialog(hDlg, TRUE);
  354.             return TRUE;
  355.             
  356.     }
  357.     return (FALSE);
  358. }
  359.  
  360.  
  361. 
  362.